7. 折れ線グラフ(2変数)

7.1. 概要

7.2. Plotlyによる作図方法

7.3. MADB Labを用いた作図例

7.3.1. 下準備

import pandas as pd
import plotly.express as px

import warnings
warnings.filterwarnings('ignore')
# 前処理の結果,以下に分析対象ファイルが格納されていることを想定
PATH_DATA = '../../data/preprocess/out/episodes.csv'
# Jupyter Book用のPlotlyのrenderer
RENDERER = 'plotly_mimetype+notebook'
def add_years_to_df(df, unit_years=10):
    """unit_years単位で区切ったyears列を追加"""
    df_new = df.copy()
    df_new['years'] = \
        pd.to_datetime(df['datePublished']).dt.year \
        // unit_years * unit_years
    df_new['years'] = df_new['years'].astype(str)
    return df_new
def show_fig(fig):
    """Jupyter Bookでも表示可能なようRendererを指定"""
    fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
    # 凡例でグラフが潰れないよう調整
    fig.update_layout(legend={
        'yanchor': 'top',
        'xanchor': 'left',
        'x': 0.01, 'y': 0.99})
    fig.show(renderer=RENDERER)
df = pd.read_csv(PATH_DATA)

7.3.2. 作品数と作家数の推移

YEARS_TO_DROP = ['1970', '2015']
df = add_years_to_df(df, 5)
df_plot = \
    df.groupby('years')['cname', 'creator'].nunique().reset_index()
# 最初と最後の年度は不十分なデータなので除外
df_plot = \
    df_plot[~df_plot['years'].isin(YEARS_TO_DROP)]\
    .reset_index(drop=True)
fig = px.line(
    df_plot, x='cname', y='creator', text='years')
fig.update_traces(textposition='bottom right')
show_fig(fig)

7.3.3. 雑誌別の作品数と作家数の推移

df = add_years_to_df(df, 5)
df_plot = \
    df.groupby(['mcname', 'years'])['cname', 'creator']\
    .nunique().reset_index()
# 最初と最後の年度は不十分なデータなので除外
df_plot = \
    df_plot[~df_plot['years'].isin(YEARS_TO_DROP)]\
    .reset_index(drop=True)
fig = px.line(
    df_plot, x='cname', y='creator', text='years',
    facet_col='mcname', facet_col_wrap=2)
fig.update_traces(textposition='bottom right')
fig.update_xaxes(title='作品数')
fig.update_yaxes(title='作家数')
show_fig(fig)